package com.hutao.medical.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.util.GraphicsMagicUtil;
import com.hutao.medical.common.util.ImagePathUtil;

@RequestMapping("/upload")
@Controller
public class FileUploadController {
	@Autowired
	private ConfigProperties configProperties;

	@ResponseBody
	@RequestMapping("/uploadLocalImg")
	public Map<String,Object> uploadLocalImg(HttpServletRequest request,HttpServletResponse response ){
		Map<String,Object> map = new HashMap<>();
		ServletInputStream input = null;
		BufferedOutputStream outStream = null;
		try {
		//String fileName = request.getHeader("-fileName");
		String fileName=new Date().getTime()+"";
		String fileType = request.getHeader("-fileType");
		String firstGuid = request.getHeader("-firstGuid");
		String mrId = request.getHeader("-mrId");
		String tempFilePath=configProperties.getServer().getServerMrDocImgFileDir();
		//缩略图路径
		String thumbnailFilePath =tempFilePath+ImagePathUtil.thumbnailPath(mrId, firstGuid)+fileName+"."+fileType;
		
		
		
		int contentLength = request.getContentLength();
		int maxFileSize = 1024*1024*2;
		if(contentLength>maxFileSize){
			map.put("ret", "tooMax");
			return map;
		}
		
		//String imagePath =tempFilePath+firstGuid+"/"+fileName+"."+fileType;
		System.out.println("- 上传路径=>"+thumbnailFilePath);
		int RLen = 1024*4;
		byte[] ReadByte = new byte[RLen];//输入流缓冲区		
		int ReadCount = 0;//记录从输入流中每次实际读取的字节数
		
		input = request.getInputStream();//取得输入流
		
		File newFileSavePath = new File(new File(thumbnailFilePath).getParent());
		if (!newFileSavePath.exists()){
			newFileSavePath.mkdirs();
		}
		 outStream = new BufferedOutputStream(new FileOutputStream(thumbnailFilePath));
		while ((ReadCount=input.readLine(ReadByte,0,RLen)) > -1){
			outStream.write(ReadByte,0,ReadCount);
		}
		outStream.flush();
		
		
		
		//jpg保存路径
		String sourceFilePath =ImagePathUtil.sourcePath(mrId, firstGuid)+fileName+".jpg";
		File sourceFile = new File(new File(tempFilePath+sourceFilePath).getParent());
		if (!sourceFile.exists()){
			sourceFile.mkdirs();
		}
		//tif图片转换为jpg
		GraphicsMagicUtil.picTransform(thumbnailFilePath, tempFilePath+sourceFilePath);
		

		ReadByte = null;
		map.put("ret", "success");
		map.put("fileName", sourceFilePath);
		} catch (Exception e) {
			map.put("ret", "exception");
		}finally{
			if(input!=null)
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			try {
				if(outStream!=null)outStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return map;
		
		
	}
}
